package com.sell.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.sell.demo.entity.Food;
import com.sell.demo.entity.Good;
import com.sell.demo.service.FoodService;
import com.sell.demo.service.GoodService;
import com.sell.demo.util.Result;

@RestController
@RequestMapping(value = "/goods")
@Api(value = "获取good Api")
public class GoodsController {
	
	@Autowired
    private GoodService goodsService;
	
	@Autowired
    private FoodService foodsService;
	
	@ApiOperation(value = "获取goodList", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/getGoods", method = RequestMethod.GET)
    public Object getGoods() {
//		 goodsList = new ArrayList<Good>();
		
		List<Good> goodsList = goodsService.getGoodList();
		for(Good good:goodsList){
			List<Food> foodList = foodsService.getFoodList(good.getId());
			good.setFoods(foodList);
		}
		
//		List<Good> goodsList = new ArrayList<Good>();
//    	Good goods = new Good();
//    	goods.setName("特色粥品");
//    	goods.setType("-1");
//    	Food foods = new Food();
//    	List<Food> foodsList = new ArrayList<Food>();
//    	foods.setName("皮蛋瘦肉粥");
//    	foods.setPrice("10");
//    	foods.setDescription("咸粥");
//    	foods.setSellCount("229");
//    	foods.setRating("100");
//    	foods.setIcon("http://fuss10.elemecdn.com/c/cd/c12745ed8a5171e13b427dbc39401jpeg.jpeg?imageView2/1/w/114/h/114");
//    	foods.setImage("http://fuss10.elemecdn.com/c/cd/c12745ed8a5171e13b427dbc39401jpeg.jpeg?imageView2/1/w/750/h/750");
//    	
//    	List<Rating> ratingsList = new ArrayList<Rating>();
//    	Rating ratings = new Rating();
//    	ratings.setUsername("3******c");
//    	ratings.setRateTime("1469281964000");
//    	ratings.setRateType("0");
//    	ratings.setText("很喜欢的粥");
//    	ratings.setAvatar("http://static.galileo.xiaojukeji.com/static/tms/default_header.png");
//    	foods.setRatings(ratingsList);
//    	foodsList.add(foods);
//    	goods.setFoods(foodsList);
//    	goodsList.add(goods);
    	
    	return JSON.toJSONString(new Result<List<Good>>(0, "", goodsList));
    }
	
	@ApiOperation(value = "获取分页goodList", httpMethod = "GET", response = String.class)
	@RequestMapping(value = "/getGoodsPage", method = RequestMethod.GET)
    public Object getGoodsPage(@RequestParam(value = "pageNum", required = true) int pageNum,@RequestParam(value = "pageSize", required = true) int pageSize) {
//		 goodsList = new ArrayList<Good>();
		
		PageInfo<Good> goodsList = goodsService.getGoodListPage(pageNum,pageSize);
		for(Good good:goodsList.getList()){
			List<Food> foodList = foodsService.getFoodList(good.getId());
			good.setFoods(foodList);
		}
    	
    	return JSON.toJSONString(new Result<PageInfo<Good>>(0, "", goodsList));
    }
	
	
	

    @RequestMapping(value = "/getGoodDetail", method = RequestMethod.GET)
    @ApiOperation(value = "获取getGoodDetail", httpMethod = "GET", response = String.class)
    public Good findOneCity(@RequestParam(value = "id", required = true) String id) {
        return goodsService.getGoodDetail(id);
    }
    
    @RequestMapping(value = "/createGood", method = RequestMethod.POST)
    @ApiOperation(value = "创建商品信息", httpMethod = "POST", response = String.class)
    public int createGood(@RequestBody Good good) {
        return goodsService.createGood(good);
    }
    
    @RequestMapping(value = "/updateGood", method = RequestMethod.POST)
    @ApiOperation(value = "更新商品信息", httpMethod = "POST", response = String.class)
    public int updateGood(@RequestBody Good good) {
        return goodsService.updateGood(good);
    }
    
    @RequestMapping(value = "/deleteGood", method = RequestMethod.GET)
    @ApiOperation(value = "删除商品信息", httpMethod = "GET", response = String.class)
    public int deleteGood(@RequestParam(value = "id", required = true) String id) {
        return goodsService.deleteGood(id);
    }
    
    @RequestMapping(value = "/createBatchGood", method = RequestMethod.POST)
    @ApiOperation(value = "创建商品信息", httpMethod = "POST", response = String.class)
    public int createBatchGood(@RequestBody List<Good> goodList) {
        return goodsService.createBatchGood(goodList);
    }
    
    @RequestMapping(value = "/updateBatchGood", method = RequestMethod.POST)
    @ApiOperation(value = "更新商品信息", httpMethod = "POST", response = String.class)
    public int updateBatchGood(@RequestBody List<Good> goodList) {
        return goodsService.updateBatchGood(goodList);
    }
    
    @RequestMapping(value = "/deleteBatchGood", method = RequestMethod.GET)
    @ApiOperation(value = "删除商品信息", httpMethod = "GET", response = String.class)
    public int deleteBatchGood(@RequestParam(value = "ids", required = true) String ids) {
        return goodsService.deleteBatchGood(ids);
    }

}
